<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <el-icon><DataLine /></el-icon>
        <span>近7天日志趋势</span>
      </div>
    </template>
    <div class="chart-container">
      <div ref="chartContainer" class="echart-container"></div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { DataLine } from '@element-plus/icons-vue'
import * as echarts from 'echarts'
import useApiStore from '@/core/apiStore'

const apiStore = useApiStore()

const chartContainer = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

const updateChart = (
  data: { log_date: string; error: number; total: number }[]
) => {
  const { trends = [] } = data
  if (chart && chartContainer.value) {
    const xAxisData = trends.map(item => item.log_date)
    const totalData = trends.map(item => item.total)
    const errorData = trends.map(item => item.error)

    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
        },
        backgroundColor: 'rgba(0,0,0,0.7)',
        padding: 10,
        textStyle: {
          fontWeight: 'bold',
        },
      },
      legend: {
        data: ['总日志', '异常日志'],
        bottom: 0,
        itemGap: 20,
        itemWidth: 10,
        itemHeight: 10,
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '15%',
        top: '5%',
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: xAxisData,
        axisLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
      },
      yAxis: {
        type: 'value',
        axisLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          lineStyle: {
            color: 'rgba(0,0,0,0.05)',
          },
        },
      },
      series: [
        {
          name: '总日志',
          type: 'line',
          smooth: true,
          symbol: 'circle',
          symbolSize: 8,
          itemStyle: {
            color: '#4361ee',
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(67, 97, 238, 0.3)',
              },
              {
                offset: 1,
                color: 'rgba(67, 97, 238, 0.1)',
              },
            ]),
          },
          data: totalData,
        },
        {
          name: '异常日志',
          type: 'line',
          smooth: true,
          symbol: 'circle',
          symbolSize: 8,
          itemStyle: {
            color: '#ef476f',
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(239, 71, 111, 0.3)',
              },
              {
                offset: 1,
                color: 'rgba(239, 71, 111, 0.1)',
              },
            ]),
          },
          data: errorData,
        },
      ],
    }

    chart.setOption(option)
  }
}

watch(apiStore.apiStates.sevenDaysTrend, newVal => {
  if (newVal.data?.data) {
    updateChart(newVal.data.data)
  }
})

onMounted(() => {
  if (chartContainer.value) {
    chart = echarts.init(chartContainer.value)

    const resizeHandler = () => chart?.resize()
    window.addEventListener('resize', resizeHandler)

    onUnmounted(() => {
      window.removeEventListener('resize', resizeHandler)
      chart?.dispose()
    })
  }
})
</script>

<style lang="scss" scoped>
.chart-container {
  position: relative;
  height: 250px;
  width: 100%;

  .echart-container {
    width: 100%;
    height: 100%;
  }
}
</style>
